using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cou;
using net_work.IService.Cou;

namespace net_work.Controllers.Cou;

[Route("api/[controller]")]
[ApiController]
public class CourseController(ILogger<CourseController> logger, ICourseService courseService) : ControllerBase
{
    /// <summary>
    /// 获取课程列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListCourses([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取课程列表");
        var result = await courseService.GetCoursesPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取课程信息
    /// </summary>
    /// <param name="courseId">课程ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetCourseById([FromQuery] long courseId)
    {
        logger.LogInformation("获取课程信息");
        return await courseService.SelectByIdAsync(courseId) is { } course
            ? AjaxResult.Success(course)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增课程
    /// </summary>
    /// <param name="courseAddRequest">新增课程实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddCourse([FromBody] CourseAddRequest courseAddRequest)
    {
        logger.LogInformation("新增课程");
        var courseId = await courseService.InsertCourse(courseAddRequest);
        return courseId > 0 ? AjaxResult.Success(new { courseId }) : AjaxResult.Error();
    }

    /// <summary>
    /// 修改课程
    /// </summary>
    /// <param name="courseUpdateRequest">修改课程实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateCourse([FromBody] CourseUpdateRequest courseUpdateRequest)
    {
        logger.LogInformation("修改课程");
        return await courseService.UpdateCourse(courseUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除课程
    /// </summary>
    /// <param name="ids">课程ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteCourse([FromBody] List<long> ids)
    {
        logger.LogInformation("删除课程");
        return await courseService.DeleteCourseByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 导出课程
    /// </summary>
    /// <returns>操作结果</returns>
    [HttpGet("export")]
    [Authorize]
    public async Task<IActionResult> ExportCourse()
    {
        logger.LogInformation("导出课程");
        var fileBytes = await courseService.ExportCourses();
        if (fileBytes.Length == 0)
        {
            return AjaxResult.Error("导出失败");
        }

        return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "课程表.xlsx");
    }

    /// <summary>
    /// 获取课程树
    /// </summary>
    /// <returns>操作结果</returns>
    [HttpGet("getTree")]
    [Authorize]
    public async Task<IActionResult> GetTree()
    {
        logger.LogInformation("获取课程树");
        var courseTree = await courseService.GetCourseTree();
        return courseTree != null ? AjaxResult.Success(courseTree) : AjaxResult.Error();
    }
}